'Public Class Matrix2DInt16
'    Sub dispose()
'        x = 1
'        y = 1
'        ReDim data(0, 0)
'    End Sub
'    Public data(,) As Int16
'    Public x, y As Integer
'    Public Sub New()
'        ReDim data(0, 0)
'        data(0, 0) = 0
'        x = 1
'        y = 1
'    End Sub
'    Public Sub New(ByVal x As Integer, ByVal y As Integer)
'        Me.x = x
'        Me.y = y
'        ReDim data(x - 1, y - 1)
'        'automatically assigned 0
'    End Sub
'    Sub New(ByVal data(,) As Int16, ByVal x As Integer, ByVal y As Integer)
'        Me.data = data
'        Me.x = x
'        Me.y = y
'    End Sub

'    Public Function Transpose() As Matrix2DInt16
'        Dim temp As New Matrix2DInt16(Me.y, Me.x)
'        Dim i, j As Integer
'        For i = 0 To Me.x - 1
'            For j = 0 To Me.y - 1
'                temp.data(j, i) = Me.data(i, j)
'            Next
'        Next
'        Return temp
'    End Function
'    Public Function Invert() As Matrix2DInt16
'        Dim temp As New Matrix2DInt16(Me.data, Me.x, Me.y)
'        Dim mayor, tmp, pivinv As Double
'        Dim n As Integer = temp.x
'        Dim indco(n - 1), indfi(n - 1), piv(n - 1) As Integer
'        Dim i, j, k, t, h, col, fil As Integer

'        For j = 0 To n - 1
'            piv(j) = 0
'        Next j
'        For i = 0 To n - 1
'            mayor = 0.0
'            For j = 0 To n - 1
'                If piv(j) <> 1 Then
'                    For k = 0 To n - 1
'                        If piv(k) = 0 Then
'                            If Math.Abs(temp.data(j, k)) >= mayor Then
'                                mayor = Math.Abs(temp.data(j, k))
'                                fil = j
'                                col = k
'                            End If
'                        ElseIf piv(k) > 1 Then
'                            MsgBox("Singular Matrix")
'                            Return temp
'                            Exit Function
'                        End If
'                    Next
'                End If
'            Next
'            piv(col) = piv(col) + 1
'            If fil <> col Then
'                For t = 0 To n - 1
'                    tmp = temp.data(fil, t)
'                    temp.data(fil, t) = temp.data(col, t)
'                    temp.data(col, t) = tmp
'                Next
'                '            tmp = x(fil)
'                '            x(fil) = x(col)
'                '            x(col) = tmp
'            End If
'            indfi(i) = fil
'            indco(i) = col
'            If temp.data(col, col) = 0 Then
'                MsgBox("Singularr Matrix")
'                Return temp
'                Exit Function
'            End If
'            pivinv = 1.0 / temp.data(col, col)
'            temp.data(col, col) = 1.0
'            For t = 0 To n - 1
'                temp.data(col, t) = temp.data(col, t) * pivinv
'            Next
'            'x(col) = x(col) * pivinv
'            For h = 0 To n - 1
'                If h <> col Then
'                    tmp = temp.data(h, col)
'                    temp.data(h, col) = 0.0
'                    For t = 0 To n - 1
'                        temp.data(h, t) = temp.data(h, t) - temp.data(col, t) * tmp
'                    Next
'                    '  x(h) = x(h) - x(col) * tmp
'                End If
'            Next
'        Next
'        For t = n - 1 To 0 Step -1
'            If indfi(t) <> indco(t) Then
'                For k = 0 To n - 1
'                    tmp = temp.data(k, indfi(t)) = temp.data(k, indco(t))
'                    temp.data(k, indco(t)) = tmp
'                Next
'            End If
'        Next
'        Return temp
'    End Function
'    Public Shared Operator *(ByVal Matrix1 As Matrix2DInt16, ByVal Matrix2 As Matrix2DInt16) As Matrix2DInt16
'            If Matrix1.y <> Matrix2.x Then
'            Return Nothing
'        Else
'            Dim ans As New Matrix2DInt16(Matrix1.x, Matrix2.y)

'            Dim a, b, c As Integer
'            For a = 0 To Matrix1.x - 1
'                For b = 0 To Matrix2.y - 1
'                    'a,b value is a row into b column
'                    ans.data(a, b) = 0
'                    For c = 0 To Matrix1.y - 1
'                        ans.data(a, b) = ans.data(a, b) + (Matrix1.data(a, c) * Matrix2.data(c, b))
'                    Next
'                Next
'            Next
'            Return ans
'        End If

'    End Operator
'    Public Shared Operator -(ByVal Matrix1 As Matrix2DInt16, ByVal Matrix2 As Matrix2DInt16) As Matrix2DInt16
'        Dim ans As New Matrix2DInt16(Matrix1.x, Matrix2.y)

'        Dim a, b
'        For a = 0 To Matrix1.x - 1
'            For b = 0 To Matrix1.y - 1
'                'a,b value is a row into b column
'                ans.data(a, b) = Matrix1.data(a, b) - Matrix2.data(a, b)
'            Next
'        Next
'        Return ans
'    End Operator
'End Class

'Public Class Matrix2Dsingle
'    Sub dispose()
'        x = 1
'        y = 1
'        ReDim data(0, 0)
'    End Sub
'    Public data(,) As Single
'    Public x, y As Integer
'    Public Sub New()
'        ReDim data(0, 0)
'        data(0, 0) = 0
'        x = 1
'        y = 1
'    End Sub
'    Public Sub New(ByVal x As Integer, ByVal y As Integer)
'        Me.x = x
'        Me.y = y
'        ReDim data(x - 1, y - 1)
'        'automatically assigned 0
'    End Sub
'    Sub New(ByVal data(,) As Single, ByVal x As Integer, ByVal y As Integer)
'        Me.data = data
'        Me.x = x
'        Me.y = y
'    End Sub

'    Public Function Transpose() As Matrix2Dsingle
'        Dim temp As New Matrix2Dsingle(Me.y, Me.x)
'        Dim i, j As Integer
'        For i = 0 To Me.x - 1
'            For j = 0 To Me.y - 1
'                temp.data(j, i) = Me.data(i, j)
'            Next
'        Next
'        Return temp
'    End Function
'    Public Function Invert() As Matrix2Dsingle
'        Dim temp As New Matrix2Dsingle(Me.data, Me.x, Me.y)
'        Dim mayor, tmp, pivinv As Double
'        Dim n As Integer = temp.x
'        Dim indco(n - 1), indfi(n - 1), piv(n - 1) As Integer
'        Dim i, j, k, t, h, col, fil As Integer

'        For j = 0 To n - 1
'            piv(j) = 0
'        Next j
'        For i = 0 To n - 1
'            mayor = 0.0
'            For j = 0 To n - 1
'                If piv(j) <> 1 Then
'                    For k = 0 To n - 1
'                        If piv(k) = 0 Then
'                            If Math.Abs(temp.data(j, k)) >= mayor Then
'                                mayor = Math.Abs(temp.data(j, k))
'                                fil = j
'                                col = k
'                            End If
'                        ElseIf piv(k) > 1 Then
'                            MsgBox("Singular Matrix")
'                            Return temp
'                            Exit Function
'                        End If
'                    Next
'                End If
'            Next
'            piv(col) = piv(col) + 1
'            If fil <> col Then
'                For t = 0 To n - 1
'                    tmp = temp.data(fil, t)
'                    temp.data(fil, t) = temp.data(col, t)
'                    temp.data(col, t) = tmp
'                Next
'                '            tmp = x(fil)
'                '            x(fil) = x(col)
'                '            x(col) = tmp
'            End If
'            indfi(i) = fil
'            indco(i) = col
'            If temp.data(col, col) = 0 Then
'                MsgBox("Singular Matrix")
'                Return temp
'                Exit Function
'            End If
'            pivinv = 1.0 / temp.data(col, col)
'            temp.data(col, col) = 1.0
'            For t = 0 To n - 1
'                temp.data(col, t) = temp.data(col, t) * pivinv
'            Next
'            'x(col) = x(col) * pivinv
'            For h = 0 To n - 1
'                If h <> col Then
'                    tmp = temp.data(h, col)
'                    temp.data(h, col) = 0.0
'                    For t = 0 To n - 1
'                        temp.data(h, t) = temp.data(h, t) - temp.data(col, t) * tmp
'                    Next
'                    '  x(h) = x(h) - x(col) * tmp
'                End If
'            Next
'        Next
'        For t = n - 1 To 0 Step -1
'            If indfi(t) <> indco(t) Then
'                For k = 0 To n - 1
'                    tmp = temp.data(k, indfi(t)) = temp.data(k, indco(t))
'                    temp.data(k, indco(t)) = tmp
'                Next
'            End If
'        Next
'        Return temp
'    End Function
'    Public Shared Operator *(ByVal Matrix1 As Matrix2Dsingle, ByVal Matrix2 As Matrix2Dsingle) As Matrix2Dsingle
'        If Matrix1.y <> Matrix2.x Then
'            Return Nothing
'        Else
'            Dim ans As New Matrix2Dsingle(Matrix1.x, Matrix2.y)

'            Dim a, b, c As Integer
'            For a = 0 To Matrix1.x - 1
'                For b = 0 To Matrix2.y - 1
'                    'a,b value is a row into b column
'                    ans.data(a, b) = 0
'                    For c = 0 To Matrix1.y - 1
'                        ans.data(a, b) = ans.data(a, b) + (Matrix1.data(a, c) * Matrix2.data(c, b))
'                    Next
'                Next
'            Next
'            Return ans
'        End If

'    End Operator
'    Public Shared Operator -(ByVal Matrix1 As Matrix2Dsingle, ByVal Matrix2 As Matrix2Dsingle) As Matrix2Dsingle
'        Dim ans As New Matrix2Dsingle(Matrix1.x, Matrix2.y)

'        Dim a, b
'        For a = 0 To Matrix1.x - 1
'            For b = 0 To Matrix1.y - 1
'                'a,b value is a row into b column
'                ans.data(a, b) = Matrix1.data(a, b) - Matrix2.data(a, b)
'            Next
'        Next
'        Return ans
'    End Operator
'End Class

Public Class Coordinate
    Public x As Int16
    Public y As Int16
    Public z As Int16
    Public Sub New(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer)
        x = CShort(i)
        y = CShort(j)
        z = CShort(k)
    End Sub
    Public Function AngleBetween(ByVal Vector2 As coordinate) As Double
        'We Use the dot product formula
        Dim sum, abs1, abs2 As Single
        sum = Me.x * Vector2.x + Me.y * Vector2.y + Me.z * Vector2.z
        abs1 = CSng((Me.x ^ 2 + Me.y ^ 2 + Me.z ^ 2) ^ (1 / 2))
        abs2 = CSng((Vector2.x ^ 2 + Vector2.y ^ 2 + Vector2.z ^ 2) ^ (1 / 2))
        Return Math.Acos(sum / (abs1 * abs2))
    End Function
    Public Function AbsoluteValue() As Double
        Return (Me.x ^ 2 + Me.y ^ 2 + Me.z ^ 2) ^ (1 / 2)
    End Function
    Public Overrides Function ToString() As String
        Return (Me.x.ToString() & "," & Me.y.ToString() & "," & Me.z.ToString())
    End Function

    Public Function IsAbsolutelyBiggerThanOrEqualTo(ByVal obj As Coordinate) As Boolean
        If (Me.x >= obj.x) And (Me.y >= obj.y) And (Me.z >= obj.z) Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
Public Class CoordinateD
    Public x As Double
    Public y As Double
    Public z As Double
    Public Sub New(ByVal i As Double, ByVal j As Double, ByVal k As Double)
        x = i
        y = j
        z = k
    End Sub
    Public Sub New(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer)
        x = i
        y = j
        z = k
    End Sub
    Public Function AngleBetween(ByVal Vector2 As CoordinateD) As Double
        'We Use the dot product formula
        Dim sum, abs1, abs2 As Single
        sum = Me.x * Vector2.x + Me.y * Vector2.y + Me.z * Vector2.z
        abs1 = CSng((Me.x ^ 2 + Me.y ^ 2 + Me.z ^ 2) ^ (1 / 2))
        abs2 = CSng((Vector2.x ^ 2 + Vector2.y ^ 2 + Vector2.z ^ 2) ^ (1 / 2))
        Return Math.Acos(sum / (abs1 * abs2))
    End Function
    Public Function AbsoluteValue() As Double
        Return (Me.x ^ 2 + Me.y ^ 2 + Me.z ^ 2) ^ (1 / 2)
    End Function
    Public Overrides Function ToString() As String
        Return (Me.x.ToString() & "," & Me.y.ToString() & "," & Me.z.ToString())
    End Function
End Class




'Public Class BinaryThresholdMask
'    Dim Matrix As Matrix3DInt16
'    Dim Minimum As Integer
'    Dim Maximum As Integer

'    Sub New(ByVal Data As Matrix3DInt16, ByVal Min As Integer, ByVal Max As Integer)
'        Matrix = New Matrix3DInt16(Data.x, Data.y, Data.z)
'        Dim i, j, k As Integer
'        Me.Minimum = Min
'        Me.Maximum = Max
'        For i = 0 To Matrix.x - 1
'            For j = 0 To Matrix.y - 1
'                For k = 0 To Matrix.z - 1
'                    If Data.data(i, j, k) <= Max And Data.data(i, j, k) >= Min Then
'                        Matrix.data(i, j, k) = 1
'                    Else
'                        Matrix.data(i, j, k) = 0
'                    End If
'                Next
'            Next
'        Next
'    End Sub

'    Public Function ReturnBandPass() As Matrix3DInt16
'        Return Matrix
'    End Function
'    Public Function ReturnBandReject() As Matrix3DInt16
'        Dim i, j, k
'        For i = 0 To Matrix.x - 1
'            For j = 0 To Matrix.y - 1
'                For k = 0 To Matrix.z - 1
'                    Matrix.data(i, j, k) = Not Matrix.data(i, j, k)
'                Next
'            Next
'        Next
'        Return Matrix
'    End Function
'End Class